package org.yxj.changethemeapp.v

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.FrameLayout
import org.yxj.changethemeapp.LogUtils


/**
 * @author  on 2024/6/25
 */
class OnTouchViewB : FrameLayout {

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context, attributeSet: AttributeSet?, defStyleRes: Int) : super(context, attributeSet, defStyleRes)

    var dx = 0f
    var isIntercept = false

    /**
     * 分发
     */
    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
        LogUtils.LOG_V("[OnTouchViewB]->dispatchTouchEvent")
        return super.dispatchTouchEvent(event)
    }


    /**
     * 拦截
     */
    override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {
        event?.let {
            when(it.action){
                MotionEvent.ACTION_DOWN->{
                    dx = it.x
                    LogUtils.LOG_V("[OnTouchViewB]->onInterceptTouchEvent is ACTION_DOWN")
                }
                MotionEvent.ACTION_MOVE->{
                    isIntercept = (it.x - this.dx) > 100
                    LogUtils.LOG_V("[OnTouchViewB]->onInterceptTouchEvent  isIntercept = ${if (isIntercept) "事假被拦击了" else "不能拦击"}")
                    return isIntercept
                }
                MotionEvent.ACTION_UP->{
                    LogUtils.LOG_V("[OnTouchViewB]->onInterceptTouchEvent is ACTION_UP")
                    isIntercept = false
                }
            }
        }
        return super.onInterceptTouchEvent(event)
    }



    /**
     * 执行
     */
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        LogUtils.LOG_V("[OnTouchViewB]->onTouchEvent")
        event?.let {
            when(it.action){
                MotionEvent.ACTION_DOWN->{
                    dx = it.x
                    LogUtils.LOG_V("[OnTouchViewB]->onTouchEvent is ACTION_DOWN")
                }
                MotionEvent.ACTION_MOVE->{
                    isIntercept = (it.x - this.dx) > 100
                    LogUtils.LOG_V("[OnTouchViewB]->onTouchEvent is ACTION_MOVE")
                }
                MotionEvent.ACTION_UP->{
                    LogUtils.LOG_V("[OnTouchViewB]->onTouchEvent is ACTION_UP")
                    isIntercept = false
                }
            }
        }
        return super.onTouchEvent(event)
    }
}